;;;;we're trying to parse text returned from queries
;;;I've decided to christen this thing: sql-ms-query-parse
(defun sql-ms-query-parse-find-dashes ()
  "Used to find the dashed line in a buffer.
  The dashed line separates the column headings and values
  in the string output of a query"
					;(interactive)
  (re-search-backward "^ [-]+[ ]*"))


(defun sql-ms-query-parse-get-column-start-end()
  "For a given position, tries to find the starting and
   ending columns(buffer positions) of a given column (sql results),
   using the dashed separator in sql text output."
  (interactive)
  (search-forward "-")
  (backward-char 1)
  (setf colstart (current-column))
  (search-forward " ")
  (if (eql (current-column) 1) (backward-char 2) (backward-char 1))
  (setf colend (- (current-column) 0))
  (list colstart colend))


       foo            bar         baz
 --------- -------------- ----------- 
       100        ming 01      jeem 1 
       200        ming 02      jeem 2 
       300           null      jeem 3 
       400        ming 04      jeem 4 

;basic flow - this gets the start and end points  of the various columns
(defun sql-ms-query-parse-get-columns-and-line()
  "Used to get the line position of the dashed line,
   and the start and end columns (buffer) of the columns (sql query)"
  (end-of-buffer)
  (sql-ms-query-parse-find-dashes)
  (end-of-line)
  (setf line-end (- (current-column) 1)) 
  (beginning-of-line)
  (setf columns '())
  (while (< (current-column) line-end)
    (progn
      (push (sql-ms-query-parse-get-column-start-end) columns)))
  (list :columns (reverse columns) :line (line-number-at-pos)))


;roight, now, we need to get the headings...
(defun foo()
  (setf columns-and-line (sql-ms-query-parse-get-columns-and-line))
  (setf headings (get-vals (- (getf columns-and-line :line) 1) (getf columns-and-line :columns)))
  (setf resultset '())
  (setf inc 5)
  (while (> (decf inc) 0)
    (push (sql-ms-query-parse-get-vals (+ (getf columns-and-line :line) inc) (getf columns-and-line :columns)) resultset)
        (push inc resultset)
    )
  ;(setf first-line (get-vals (+ (getf columns-and-line :line) 1) (getf columns-and-line :columns)))
  (list :headings headings :data resultset))


(defun sql-ms-query-parse-get-vals (line cols)
  (goto-line line)
  (setf values '())
  (setf colcount 0)
  (while (> (length cols) 0)
    (progn
      (beginning-of-line)
      (setf se (pop cols))
      (forward-char (first se))
      (setf mystart (point))
      (beginning-of-line)
      (forward-char (second se))
      (setf myend (point))
      (setf whole-match (buffer-substring-no-properties mystart myend))
      (setf whole-match (sql-ms-query-parse-chomp whole-match))
      (message (concat "->" whole-match))
      (push colcount values)
      (push whole-match values)
      (incf colcount)))
  (reverse values))



(defun sql-ms-query-parse-chomp (str)
  "This function has been stolen off the web
I think the right person should get credit for it
but I can't for the LIFE of me remember where I found it..."
  (let ((s (if (symbolp str)(symbol-name str) str)))
    (save-excursion
      (while (and
	      (not (null (string-match "^\\( \\|\f\\|\t\\|\n\\)" s)))
	      (> (length s) (string-match "^\\( \\|\f\\|\t\\|\n\\)" s)))
	(setq s (replace-match "" t nil s)))
      (while (and
	      (not (null (string-match "\\( \\|\f\\|\t\\|\n\\)$" s)))
	      (> (length s) (string-match "\\( \\|\f\\|\t\\|\n\\)$" s)))
	(setq s (replace-match "" t nil s))))
    s))

(defun sql-ms-query-parse-listtohash (list)
  "converts the list containing integers and string values to
a hash table, so you can refer to columns(SQL QUERY) by name
instead of index"
  (setf h (make-hash-table :test 'equal))
  (while (> (length list) 0)
    (puthash (pop list) (pop list) h))
  h)


(setq sql-user
	    (read-from-minibuffer "User: " sql-user nil nil
				  sql-user-history))